package com.jpx.jxcbiz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.common.dto.ReturnAddDto;
import com.jpx.common.dto.ReturnDto;
import com.jpx.common.dto.ReturnGoodsAddDto;
import com.jpx.common.vo.ResultEntity;
import com.jpx.jxcbiz.entity.*;
import com.jpx.jxcbiz.service.ITReturnListGoodsService;
import com.jpx.jxcbiz.service.ITReturnListService;
import com.jpx.jxcbiz.service.ITUserService;
import com.jpx.jxcbiz.vo.PurchaseReceiptQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张洋洋
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/biz/return")
public class TReturnListController {

    @Autowired
    ITReturnListService returnListService;

    @Autowired
    ITReturnListGoodsService returnListGoodsService;

    @Autowired
    ITUserService userService;

    /**
     * 退货单据查询
     * @param returnDto
     * @return
     */
    @RequestMapping("/returnQuery")
    public ResultEntity list(ReturnDto returnDto){
        List<PurchaseReceiptQuery> list = returnListService.returnDocumentQuery(returnDto);
        return ResultEntity.ok(list);
    }


    /**
     * 根据退货单id查询所有退货单商品
     * @param returnListId
     * @return
     */
    @RequestMapping("/listGoods")
    public ResultEntity listGoods(Integer returnListId){
        if(returnListId==null){
            return null;
        }
        List<TReturnListGoods> returnListGoodsList = returnListGoodsService.selectGoodsById(returnListId);
        return ResultEntity.ok(returnListGoodsList);
    }

    /**
     * 根据id删除退货单信息 包括退货单里的商品
     * @param returnListId
     * @return
     */
    @RequestMapping("/delete")
    public ResultEntity delete(Integer returnListId){
        Wrapper<TReturnListGoods> wrapper = new EntityWrapper<>();
        wrapper.eq("return_list_id",returnListId);
        /*先删除退货单里的商品*/
        returnListGoodsService.delete(wrapper);
        /*再删除退货单信息*/
        returnListService.deleteById(returnListId);
        return ResultEntity.ok();
    }

    /**
     * 退货
     * @return
     */
    @RequestMapping("/returnGoods")
    public ResultEntity returnGoods(@RequestBody ReturnAddDto returnAddDto){
        //根据用户名查找用户ID
        String username = returnAddDto.getUsername();
        Wrapper<TUser> wrapper = new EntityWrapper();
        wrapper.eq("user_name",username);
        TUser user = userService.selectOne(wrapper);

        //添加退货表
        TReturnList returnList = new TReturnList();
        BeanUtils.copyProperties(returnAddDto,returnList);
        returnList.setUserId(user.getId());
        returnListService.save(returnList);

        List<ReturnGoodsAddDto> list = returnAddDto.getList();

        //遍历退货信息 添加到表
        for (ReturnGoodsAddDto listGoods : list ) {
            TReturnListGoods returnListGoods = new TReturnListGoods();
            returnListGoods.setGoodsId(listGoods.getId());
            returnListGoods.setReturnListId(returnList.getId());
            returnListGoods.setCode(listGoods.getCode());
            returnListGoods.setModel(listGoods.getModel());
            returnListGoods.setName(listGoods.getName());
            returnListGoods.setNum(listGoods.getNum());
            returnListGoods.setPrice(listGoods.getSellingPrice());
            returnListGoods.setTotal(listGoods.getNum()*listGoods.getSellingPrice());
            returnListGoods.setTypeId(listGoods.getTypeId());
            returnListGoods.setUnit(listGoods.getUnit());
            //添加
            returnListGoodsService.insert(returnListGoods);
        }

        return ResultEntity.ok();
    }

}
